package com.kaifamiao.month05.day07;

import com.kaifamiao.month04.day27.CutThread;
import lombok.Data;

import java.io.*;

@Data
// 文件切割多线程切割
public class FileCut {

    private long size; // 分成每个线程的大小
    private File original;  // 原文件
    private File directory; // 分割之后的文件
    private int count; // 分割的数量

    private long n; // 单次分割的大小

    public FileCut(File original, File directory, int count, long n) {
        this.original = original;
        this.directory = directory;
        this.count = count;
        this.n = n;
    }

    public void cutFile() throws IOException {
        long begin = 0;  // 当前线程开始切割的位置
        size = (int)Math.ceil(original.length() / (count * 1.0));
        for (int i = 0; i < count; i++) {
            if ( i == count - 1 ) {
                size = original.length() - size * i; // 最后一个线程裁剪的大小
            }
            new CutThread("线程" + (i +1 ),original,directory,begin,n, size).start();
            begin = begin + size;
        }
    }

}
